{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bb', 1, 'aaa']\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "##from empty import Empty\n",
    "\n",
    "class CircularQueue(object):\n",
    "    class _Node(object):\n",
    "        __slots__= '_elem','_next'\n",
    "        def __init__(self,elem,next):\n",
    "            self._elem=elem\n",
    "            self._next=next\n",
    "    def __init__(self):\n",
    "        \"\"\"\n",
    "        Create an empty queue\n",
    "        \"\"\"\n",
    "        self._tail=None\n",
    "        self._size=0\n",
    "    def __len__(self):\n",
    "        \"\"\"\n",
    "        Return the number of elements in Queue\n",
    "        \"\"\"\n",
    "        return self._size\n",
    "    def is_empty(self):\n",
    "        \"\"\"\n",
    "        Return True if the Queue is empty\n",
    "        \"\"\"\n",
    "        return self._size ==0\n",
    "    \n",
    "    def first(self):\n",
    "        \"\"\"\n",
    "        Return the first element at the front of the Queue\n",
    "        \"\"\"\n",
    "        if self.is_empty():\n",
    "            raise Empty(\"Queue is empty\")\n",
    "        head = self._tail._next\n",
    "        return head._elem\n",
    "    \n",
    "    def dequeue(self):\n",
    "        \"\"\"\n",
    "        Remove and Return the element at the front of the Queue\n",
    "        \n",
    "        \"\"\"\n",
    "        if self.is_empty():\n",
    "            raise Empty(\"Queue is empty\")\n",
    "        oldhead = self._tail._next\n",
    "        if self._size==1:\n",
    "            self._tail=None\n",
    "        else:\n",
    "            self._tail._next=oldhead._next\n",
    "        self._size-=1\n",
    "        return oldhead._elem\n",
    "    \n",
    "    def enqueue(self,e):\n",
    "        \"\"\"\n",
    "        Add an element to the back of the queue\n",
    "        \"\"\"\n",
    "        new=self._Node(e,None)\n",
    "        if self.is_empty():\n",
    "            new._next=new\n",
    "        else:\n",
    "            new._next=self._tail._next\n",
    "            self._tail._next=new\n",
    "        self._tail=new\n",
    "        self._size += 1\n",
    "    \n",
    "    def rotate ( self ):\n",
    "        if self . _size > 0:\n",
    "            self . _tail = self . _tail . _next\n",
    "        \n",
    "    def __repr__(self):\n",
    "        L=[]\n",
    "        if self.is_empty():\n",
    "            return f'{L}'\n",
    "        else:\n",
    "            current =self._tail._next\n",
    "            while current._next is not self._tail._next:\n",
    "                L.append(current._elem)\n",
    "                current = current._next\n",
    "            L.append(current._elem)\n",
    "            return f'{L}'\n",
    "    \n",
    "if __name__=='__main__':\n",
    "    q=CircularQueue()\n",
    "    q.enqueue('bb')\n",
    "    q.enqueue(1)\n",
    "    q.enqueue('aaa')\n",
    "    print(q)\n",
    "    q.dequeue()\n",
    "    q.dequeue()\n",
    "    q.dequeue()\n",
    "    print(q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
